/*
 * JobApplicationNotifier.java
 *
 * Created on 13. Juni 2007, 04:36
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package sbc.corso;

import corso.lang.*;
import java.util.*;
import sbc.fact.*;
import sbc.repos.*;

/**
 *
 * @author mustermark
 */
public abstract class JobApplicationNotifier implements Runnable {
        
    private CorsoData data;
    private CorsoNotification notification;
    private JobApplication regObj;
    private CorsoVarOid regOid;
    private JobApplicationReceiver receiver;


    public JobApplicationNotifier(String regObjectName, String site, CorsoConnection con, JobApplicationReceiver rec) {
        receiver = rec;
        
        // create object for registration **************************************
        regObj = new JobApplication();

        // create a named oid or get it, if it yet exists **********************
        try  { 
            //regOid = con.getOrCreateNamedVarOid (con.getCurrentStrategy(), regObjectName, null);
            regOid = con.getNamedVarOid (regObjectName,
                                           site,
                                           null,
                                           false,
                                           CorsoConnection.NO_TIMEOUT);
            
        } catch(CorsoException e) {
            System.out.println("Unknown RegObject: " + regObjectName);
            System.out.println(e);
            return;
        }

        // create notification on this object **********************************
        Vector notifVec = new Vector ();

        CorsoNotificationItem regNotifItem = new CorsoNotificationItem (regOid, CorsoNotificationItem.INITIALIZE_WITH_CURRENT_TIMESTAMP, CorsoNotificationItem.CURRENT_TIMESTAMP);

        notifVec.addElement (regNotifItem);
        try {
            notification = con.createNotification (notifVec, con.getCurrentStrategy());
        }  catch(CorsoException e) {
            System.out.println("Unable to create Notification");
            System.out.println(e);
        }
            
        

        data = con.createData();
        System.out.println ("Registry " + regObjectName + " ready...");
    }




    public void run() {

        for(;;) {
            try {
                // now waiting for writing regObj or a data object *******************
                System.out.println ("waiting for registration or data");
                CorsoNotificationItem fired = notification.start (CorsoConnection.INFINITE_TIMEOUT, data);

                if (fired != null) {
                    if (fired.varOid ().equals(regOid)) {
                        // a new data object was registered ******************************
                        // so read the new oid and add it to the notification ************
                        System.out.println ("someone wants to register a new object");

                        // get object from oid *******************************************
                        regOid.readShareable (regObj, null, CorsoConnection.NO_TIMEOUT);

                        // get new data oid from object **********************************
                        CorsoVarOid newDataOid = regObj.getOid();

                        // create notification item on the new data oid ******************
                        CorsoNotificationItem newNotifItem = new CorsoNotificationItem (newDataOid, 0, CorsoNotificationItem.CURRENT_TIMESTAMP);

                        // add notification item to notification *************************
                        notification.addItem (newNotifItem, CorsoConnection.NO_TIMEOUT);
                        System.out.println ("ok, registration was sucessfull");

                        int number = notification.getItems ().size ();
                        System.out.print ("i am listening to 1 registration object ");
                        System.out.println ("and "+ (number - 1) + " data objects");
                        System.out.println ("-------------------------------");
                    } else {
                        // a registered data object was written **************************
                        System.out.println ("someone has written the data object");

                        // read the data and display it **********************************
                        //Sample8Data dataObj = new Sample8Data ();
                        //Board boardObj = new Board();
                        CorsoShareable shareable = createEmptyShareable();
                        //Module mod = new Module();
                        fired.varOid().readShareable (shareable, null, CorsoConnection.NO_TIMEOUT);
                        //dataObj.print ();
                        //list.add(shareable);
                        //receiver.shareableRegistered(shareable);
                    }
                }
            } catch(Exception e) { 
                System.out.println("Error notifying " + regOid);
                System.out.println(e);
            }
        }

    }
    
    protected abstract CorsoShareable createEmptyShareable();
}
